import Toast from '../../../@vant/weapp/toast/toast';
import {IMG_PATH,apiList,ApiRootUrl} from '../../../config/index.js'
import {request,isPhone,isName} from "../../../utils/util"
//获取应用实例
const app = getApp()
Page({
  data: {
    IMG_PATH:IMG_PATH,
    statusBarHeight:'',
    active:0,
    loadingFlag:false,
    topbg:"rgba(0,0,0,0)",
    width:wx.getSystemInfoSync().windowWidth,
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    //绑定宫格图片地址和文字
    dataList:[],
    customFlag:false,
    value:'',
    infoList:[],
    imageurlList:[],
    chooseMulitUser:false,
    columnsUser:[],
  },
  onLoad: function () {
    this.setData({
      statusBarHeight:wx.getSystemInfoSync()['statusBarHeight'],
      userinfo:wx.getStorageSync('userinfo')
    })
    let dataList = [
      {
      imgUrl:`/icon/fuwuzixun.png`,
      textValue:'劳动咨询',
      navigateUrl:"/pages/consultation/company/index"
      // navigateUrl:""
    },
    {
      imgUrl:`/icon/renyuanguanli.png`,
      textValue:'人员管理',
      navigateUrl:"../personlprove/personlprove"
    },
    {
      imgUrl:`/icon/weijidengji.png`,
      textValue:'违纪登记',
      navigateUrl:""
    },{
      imgUrl:`/icon/yunduanxuncha.png`,
      textValue:'云端巡查',
      navigateUrl:""
    },{
      imgUrl:`/icon/zhaopingdengji.png`,
      textValue:'招聘登记',
      navigateUrl:""
    },
    {
      imgUrl:`/icon/wodexiaoxi.png`,
      textValue:'我的信息',
      navigateUrl:""
    }
  ];
    let imageurlList = [{
      id:'1',
      url:`${IMG_PATH}/center/laowu_lunbo.png`
    },{
      id:'2',
      url:`${IMG_PATH}/center/laowu_lunbo.png`
    },{
      id:'3',
      url:`${IMG_PATH}/center/laowu_lunbo.png`
    }];
    this.setData({
      dataList:dataList,
      imageurlList:imageurlList
    })
  },
  onChange(event) {
    console.log(event.detail)
    if(event.detail == 2){
      console.log("点击新建")
    }else{
      this.setData({ active: event.detail });
    }
    
  },
  Tapzhuchang(){
    this.checkRegister()
  },
  checkRegister() {
    let self = this;
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/index/checkAppManagerUser/${openId}`;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        Toast.clear();
        console.log("res=========>",res)
        if (res.data.code == 20000) {
          let data = res.data.data;
          if(data.length > 1){
            let list = []
            data.forEach((item)=>{
              list.push(
                {
                  openId:item.openId,
                  laobourCode:item.labourCode,
                  componentCode:item.componentCode,
                  componentName:item.componentName,
                  text:item.name+'-'+item.componentName,
                }
              )
            })
            self.setData({
              chooseMulitUser:true,
              columnsUser:list
            })
            return
          }else if(data.length == 1){
            let list = []
            data.forEach((item)=>{
              list.push(
                {
                  openId:item.openId,
                  laobourCode:item.labourCode,
                  componentCode:item.componentCode,
                  componentName:item.componentName,
                  text:item.name+'-'+item.componentName,
                }
              )
            })
            self.setData({
              chooseMulitUser:true,
              columnsUser:list
            })
          }
          return
          
        } else if (res.data.code == 30000) {
          self.setData({
            tishiShow: true,
          })
        } else if (res.data.code == 30002) {
          // self.initCompany();
          // self.initService();
          self.setData({
            tishiShow: true,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  onCloseUserPopup(){
    this.setData({
      chooseMulitUser:false
    })
  },
  onChangeUser(){
    this.setData({
      chooseMulitUser:false
    })
  },
  onConfirmUser(event){
    let value = wx.getStorageSync('userinfo')
    console.log(value)
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.doLoginAppManagerUser+`?openId=${value.openId}&labourCode=${value.labourCode}&componentCode=${value.componentCode}`,
    {},"GET").then(res =>{
      Toast.clear();
      console.log(res)
      let data1 = res.data
      wx.setStorageSync('userinfo', data1)
      // 无user表示是劳务工
      if (!data1.user) {
        // wx.redirectTo({
        //   url: '../firstpage/firstpage',
        // })

      } else {
        console.log("data.user===>", data1.user)
        // 有user表示是非劳务工
        if (data1.user.typeCode == 'business') {
          wx.redirectTo({
            url: '/pages/companyfirstpage/companyfirstpage',
          })
        } else if (data1.user.typeCode == 'labour') {
          wx.redirectTo({
            url: '/pages/laborcompany/firstpage/firstpage',
          })
        } else if (data1.user.typeCode == 'society') {
          wx.redirectTo({
            url: '/pages/laborcompany/firstpage/firstpage'
          });
        }
      }
    }).catch(err =>{
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
      });
    })
    return
    this.setData({
      chooseMulitUser:false
    })
  },
  newAddTap(event){
    console.log("点击新建")
  },
  //点击信息触发事件
  tomessage(){
   
   
  },
  msgTap(){
    wx.requestSubscribeMessage({
      tmplIds: ['8Dqe1JDHYbv4SP4bGBgIL6a9oS9RIbQlkOaqGt0QNUw','kPrOW5nKcf2_gYzpUkn_HX_O3lIzfCmExMfCKHvvjU8'],
      success (res) {
        console.log("-------->",res)
      },
      fail(err){
        console.log("-------->",err)
      }
    })
    // wx.navigateTo({
    //   url: '../message/messagePush/messagePush',
    // })
  },
  //点击搜索框触发事件
  onSearch(event){
    Toast(`${event.detail}`);
    console.log(event)
  },
  //点击图片触发事件
  imageTab(event){
    console.log(event.target.dataset.id);
  },
  //监听用户下拉刷新事件
  onPullDownRefresh(){
    wx.showNavigationBarLoading();
    setTimeout(function(){
      wx.hideNavigationBarLoading(); //完成停止加载图标
      wx.stopPullDownRefresh();
    },5000)
  },
  /**
  * 页面滑动监听
  */
 onPageScroll:function(e){
   console.log(e.scrollTop)
   if(e.scrollTop >10){
     this.setData({
       topbg:"#ACDAC3"
     })
   }else{
     this.setData({
       topbg:"rgba(0,0,0,0)"
     })
   }
 },
})
